/**
  ******************************************************************************
  * @file    main.c
  * @author  AE Team
  * @version 1.0.3
  * @date    2024-05-28
  * @brief   本样例演示GTIM1的编码计数功能，采用正交x2模式，在TI1FP1边沿计数
  *
  * @note    硬件平台：SYM32L010_StartKit_V1.0
  *          接线说明：断开J13、J19、J23、J27
  *                    使用杜邦线将PA03连接到PA06，将PA04连接到PA05
  *                    PA03 <==> PA06
  *                    PA04 <==> PA05
  *
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2024 SIYIMicro.
  * All rights reserved.</center></h2>
  *
  *
  ******************************************************************************
  */


/******************************************************************************/
/* Include files                                                              */
/******************************************************************************/
#include "main.h"
#include "sym32l010_hal_sysctrl.h"
#include "sym32l010_hal_systick.h"
#include "sym32l010_hal_gpio.h"
#include "sym32l010_hal_gtim.h"
#include "sym32l010_startkit_board.h"


/******************************************************************************/
/* Local pre-processor symbols/macros ('#define')                             */
/******************************************************************************/

/******************************************************************************/
/* Global variable definitions (declared in header file with 'extern')        */
/******************************************************************************/
GTIM_HandleTypeDef hgtim1;

/******************************************************************************/
/* Local type definitions ('typedef')                                         */
/******************************************************************************/

/******************************************************************************/
/* Local function prototypes ('static')                                       */
/******************************************************************************/
static void GPIO_Init(void);
static void GTIM1_Init(void);

/******************************************************************************/
/* Local variable definitions ('static')                                      */
/******************************************************************************/

/******************************************************************************/
/* Local pre-processor symbols/macros ('#define')                             */
/******************************************************************************/

/******************************************************************************/
/* Function implementation - global ('extern') and local ('static')           */
/******************************************************************************/


/*******************************************************************************
 ** \brief  Main function of project
 **
 ** \return uint32_t return value, if needed
 **
 **
 ******************************************************************************/
int32_t main(void)
{
    // 系统时钟初始化
    HAL_SysCtrl_ConfigWaitCycle(48000000);
    HAL_SysCtrl_HSI_SetDiv(HSIOSC_TO_HSI48MHZ);

    // 初始化SysTick
    HAL_InitTick(48000000);

    // GPIO 初始化
    GPIO_Init();

    // GTIM1 初始化
    GTIM1_Init();

    //------------------------------------------------------------
    // 需将PA03连接到PA06，将PA04连接到PA05
    // 以下代码更改PA03/PA04的电平，以实现计数
    while (1)
    {
        for (uint8_t i=0; i<4; i++)                                   // 正向计数8次
        {
            PA03_SETHIGH();
            FirmwareDelay(10);
            PA04_SETHIGH();
            FirmwareDelay(10);
            PA03_SETLOW();
            FirmwareDelay(10);
            PA04_SETLOW();
            FirmwareDelay(10);
        }
        FirmwareDelay(10);                                            // 请在此处查看计数值

        for (uint8_t i=0; i<5; i++)                                   // 反向计数10次
        {
            PA04_SETHIGH();
            FirmwareDelay(10);
            PA03_SETHIGH();
            FirmwareDelay(10);
            PA04_SETLOW();
            FirmwareDelay(10);
            PA03_SETLOW();
            FirmwareDelay(10);
        }
        FirmwareDelay(10);                                            // 请在此处查看计数值

    }
}


/*******************************************************************************
 * @brief  GPIO 初始化
 * @retval None
 */
static void GPIO_Init(void)
{
    GPIO_InitTypeDef         GPIO_InitStruct;
    MemClr(&GPIO_InitStruct, sizeof(GPIO_InitStruct));

    HAL_SYSCTRL_GPIOA_CLK_ENABLE();

    // 初始化PA05/PA06为编码输入管脚
    AFx_GTIM1CH1_PA06();
    AFx_GTIM1CH2_PA05();
    GPIO_InitStruct.Pins = GPIO_PIN_5 | GPIO_PIN_6;
    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
    HAL_GPIO_Init(SYM_GPIOA, &GPIO_InitStruct);

    // 初始化PA03/PA04为通用IO输出, 输出波形作为编码器的输入
    HAL_GPIO_WritePins(SYM_GPIOA, GPIO_PIN_3 | GPIO_PIN_4, GPIO_PIN_RESET);
    GPIO_InitStruct.Pins = GPIO_PIN_3 | GPIO_PIN_4;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    HAL_GPIO_Init(SYM_GPIOA, &GPIO_InitStruct);
}


/*******************************************************************************
 * @brief  GTIM1 初始化
 * @retval None
 */
static void GTIM1_Init(void)
{
    GTIM_ClockConfigTypeDef     GTIM_ClkCgfStruct;
    GTIM_Encoder_InitTypeDef    GTIM_EncoderStruct;
    MemClr(&GTIM_ClkCgfStruct,  sizeof(GTIM_ClkCgfStruct));
    MemClr(&GTIM_EncoderStruct, sizeof(GTIM_EncoderStruct));

    HAL_SYSCTRL_GTIM1_CLK_ENABLE();

    // 初始化GTIM1时基
    MemClr(&hgtim1, sizeof(hgtim1));
    hgtim1.Instance           = SYM_GTIM1;
    hgtim1.Init.Period        = 0xFFFF;
    hgtim1.Init.Prescaler     = 48 - 1;
    hgtim1.Init.CounterMode   = GTIM_COUNTERMODE_UP;

    // 初始化GTIM的Encoder接口
    GTIM_EncoderStruct.EncoderMode  = GTIM_ENCODERMODE_TI1;           // 正交x2模式，在TI1FP1边沿计数
    GTIM_EncoderStruct.IC1Filter    = 2;
    GTIM_EncoderStruct.IC1Polarity  = GTIM_ENCODERINPUTPOLARITY_RISING;
    GTIM_EncoderStruct.IC1Prescaler = GTIM_ICPSC_DIV1;
    GTIM_EncoderStruct.IC1Selection = GTIM_ICSELECTION_DIRECTTI;
    GTIM_EncoderStruct.IC2Filter    = 2;
    GTIM_EncoderStruct.IC2Polarity  = GTIM_ENCODERINPUTPOLARITY_RISING;
    GTIM_EncoderStruct.IC2Prescaler = GTIM_ICPSC_DIV1;
    GTIM_EncoderStruct.IC2Selection = GTIM_ICSELECTION_DIRECTTI;
    HAL_GTIM_Encoder_Init(&hgtim1, &GTIM_EncoderStruct);

    HAL_GTIM_Encoder_Start(&hgtim1, GTIM_CHANNEL_1 | GTIM_CHANNEL_2); // 启动GTIM的Encoder
}


/*******************************************************************************
 ** EOF (not truncated)
 ******************************************************************************/
#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
    /* USER CODE BEGIN */
    /* User can add his own implementation to report the file name and line number,
       tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
    /* USER CODE END */
}
#endif /* USE_FULL_ASSERT */


/************************ (C) COPYRIGHT SIYIMicro *****END OF FILE*************/
